"use client";

import { Card } from "@/components/ui/card"
import Image from "next/image"
import Link from "next/link"
import { Eye, Heart } from "lucide-react"
import { pickDefaultIconForKey } from "@/constants/default-icons"

interface ToolCardProps {
  name: string
  icon: string
  summary?: string
  description?: string
  category?: string
  id?: string
  viewCount?: number
  favoritesCount?: number
}

export function ToolCard({ name, icon, summary, description, category, id, viewCount, favoritesCount }: ToolCardProps) {
  const slug = id || name.toLowerCase().replace(/[^a-z0-9\u4e00-\u9fa5]+/g, "-")
  const stripHtml = (html: string) => html.replace(/<[^>]+>/g, "").trim();
  const finalIcon = icon && icon.startsWith("http") ? icon : pickDefaultIconForKey(name);
  const displayText = stripHtml(summary ?? description ?? "");

  return (
    <Link href={`/contentDetail?id=${slug}`} target="_blank" rel="noopener noreferrer">
      <Card className="group p-4 h-full hover:shadow-lg transition-all duration-300 cursor-pointer border border-border hover:border-primary/30 flex flex-col">
        <div className="flex items-start gap-3 flex-1">
          <div className="flex-shrink-0 w-12 h-12 rounded-lg bg-gradient-to-br from-primary/10 to-primary/5 flex items-center justify-center overflow-hidden">
            <Image src={finalIcon} alt={name} width={48} height={48} className="object-cover" />
          </div>

          <div className="flex-1 min-w-0 flex flex-col">
            <h3 className="font-medium text-sm text-foreground group-hover:text-primary transition-colors mb-1 truncate">
              {name}
            </h3>
            <p className="text-xs text-muted-foreground line-clamp-2 leading-[1.4]">{displayText}</p>
          </div>
        </div>
        <div className="flex items-center justify-end gap-4 text-xs text-muted-foreground mt-2 pt-2 border-t border-border/50">
          <div className="flex items-center gap-1">
            <Eye className="w-3.5 h-3.5" />
            <span>{viewCount || 0}</span>
          </div>
          <div className="flex items-center gap-1">
            <Heart className="w-3.5 h-3.5" />
            <span>{favoritesCount || 0}</span>
          </div>
        </div>
      </Card>
    </Link>
  )
}
